package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.InfoQuestionDao;
import com.gusha.model.InfoQuestion;
import com.gusha.model.User;


/**
 * @Created：2018-11-08 10:07:03
 * @Author LightSound的代码生成器
 * @Description:InfoQuestionService服务类
 */

@Service
@Transactional
public class InfoQuestionService {

    @Autowired
    private InfoQuestionDao infoQuestionDao;

    public Page<InfoQuestion> findAll(Pageable pageable, InfoQuestion infoQuestion) {
        @SuppressWarnings("serial")
        Page<InfoQuestion> result = infoQuestionDao.findAll(new Specification<InfoQuestion>() {
            @Override
            public Predicate toPredicate(Root<InfoQuestion> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (infoQuestion.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),infoQuestion.getId()));
                }
                if (infoQuestion.getUser() != null) {
                    list.add(cb.equal(root.get("user").as(User.class),infoQuestion.getUser()));
                }
                if (infoQuestion.getQuestionId() != null) {
                    list.add(cb.equal(root.get("questionId").as(BigInteger.class),infoQuestion.getQuestionId()));
                }
                if (StringUtils.isNotBlank(infoQuestion.getQuestionAnswer())) {
                    list.add(cb.equal(root.get("questionAnswer").as(String.class),infoQuestion.getQuestionAnswer()));
                }
                if (infoQuestion.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),infoQuestion.getCreateTime()));
                }
                if (infoQuestion.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),infoQuestion.getUpdateTime()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
